package com.zretc.emptraining.daoimpl;

import java.util.List;

import com.zretc.emptraining.dao.CourseEvaDao;
import com.zretc.emptraining.dbtools.DBTools;
import com.zretc.emptraining.dbtools.PageData;
import com.zretc.emptraining.entity.CourseEva;

public class CourseEvaDaoImpl implements CourseEvaDao {

	@Override
	public boolean addCourseEva(CourseEva courseEva) {
		String sql = "insert into courseeva values (null,?,?,now(),?,?)";
		int n = DBTools.getUpdate(sql,courseEva.getEmpNo(), courseEva.getCpNo(), courseEva.getEvalContent(),
				courseEva.getEvalState());
		return n > 0;
	}
	
	@Override
	public List<CourseEva> getCourseEvaAll() {
		String sql = "SELECT evalNo,cs.empNo,e.empName,cs.cpNo,c.courseName,evalDate,evalContent,evalState\r\n" + 
				"FROM courseeva cs LEFT JOIN emp e ON cs.empNo = e.empNo\r\n" + 
				"LEFT JOIN courseplan cp ON cp.cpNo = cs.cpNo\r\n" + 
				"LEFT JOIN course c ON c.courseNo = cp.courseNo";
		return (List<CourseEva>) DBTools.exQuery(sql, CourseEva.class, null);
	}

	@Override
	public PageData<CourseEva> getCourseEvaByPage(int page, int pageSize, String courseName) {
		String sql = "SELECT evalNo,cs.empNo,e.empName,cs.cpNo,c.courseName,evalDate,evalContent,evalState\r\n" + 
				"FROM courseeva cs LEFT JOIN emp e ON cs.empNo = e.empNo\r\n" + 
				"LEFT JOIN courseplan cp ON cp.cpNo = cs.cpNo\r\n" + 
				"LEFT JOIN course c ON c.courseNo = cp.courseNo WHERE courseName like ? ";
		return DBTools.exQueryByPage(sql, CourseEva.class, page, pageSize, "%" + courseName + "%");
	}

	@Override
	public boolean updCourseEva(CourseEva courseEva) {
		String sql = "update courseeva set evalState=? where evalNo=?";
		int n = DBTools.getUpdate(sql, courseEva.getEvalState(),courseEva.getEvalNo());
		return n > 0;
	}

	@Override
	public boolean delCourseEva(int evalNo) {
		String sql = "delete courseeva where evalNo=?";
		int n = DBTools.getUpdate(sql, evalNo);
		return n > 0;
	}
	
	@Override
	public PageData<CourseEva> getCourseEvaByCpNo(int page, int pageSize, int cpNo) {
		String sql = "SELECT courseeva.evalNo,courseeva.empNo,(select empName from emp where emp.empNo=courseeva.empNo)as empName,(select empPic from emp where emp.empNo=courseeva.empNo)as empPic,courseeva.cpNo,\r\n"
				+ "(SELECT course.courseName from  course WHERE course.courseNo=\r\n"
				+ "(select courseplan.courseNo from courseplan where courseplan.cpNo=courseeva.cpNo))as courseName,\r\n"
				+ "courseeva.evalDate,courseeva.evalContent,courseeva.evalState FROM courseeva where cpNo = ? ORDER BY courseeva.evalDate DESC";
		return DBTools.exQueryByPage(sql, CourseEva.class, page, pageSize, cpNo);
	}

}
